@tool
class_name EntityData_Character
extends EntityData

#region 属性
#endregion
#region 字段
@export_group("Attribute")
@export var default_weapon_id:String = "10001":
	get:
		return default_weapon_id
	set(value):
		default_weapon_id = value
		data_changed.emit("attribute","default_weapon_id")
@export_group("")
@export_group("Camp")
@export var default_camp:GameEnum.Camp = GameEnum.Camp.None:
	get:
		return default_camp
	set(value):
		default_camp = value
		data_changed.emit("attribute","default_camp")
@export_group("")
@export_group("Anim")
@export var default_anim_name:String = "Idle":
	get:
		return default_anim_name
	set(value):
		default_anim_name = value
@export_group("")
@export_group("Other")
@export var sprite:Texture2D:
	get:
		return sprite
	set(value):
		sprite = value
@export_group("")
#endregion
#region 信号
#endregion
#region 运行时属性
#var runtime_camp:GameEnum.Camp:
	#get:
		#return runtime_camp
	#set(value):
		#if runtime_camp != value:
			#GameCampSystem.group_remove(runtime_camp,entity.root)
			#runtime_camp = value
			#GameCampSystem.group_add(runtime_camp,entity.root)
			#data_changed.emit("attribute","runtime_camp")
#endregion

#region 基类方法
## 初始化运行时属性
func init(entity:Entity):
	if inited:
		return
	super(entity)
	## 初始化属性
	add_attribute_value("Health",CompositeProperty.PropertyType.Int,get_attribute_value("MaxHealth"))
	## 未受百分比影响的固定速度
	add_attribute_value("BaseVel",CompositeProperty.PropertyType.Vector2,Vector2.ZERO)
	## 实际速度
	add_attribute_value("Vel",CompositeProperty.PropertyType.Vector2,Vector2.ZERO)
	add_attribute_value("Acc",CompositeProperty.PropertyType.Vector2,Vector2.ZERO)
	## 具体的Camp通过CampManager初始化
	add_attribute_value("Camp",CompositeProperty.PropertyType.Camp,GameEnum.Camp.None)
	## 体力
	add_attribute_value("Stamina",CompositeProperty.PropertyType.Float,0)
	
func _on_attribute_changed(attribute:Attribute,old_value,new_value):
	super(attribute,old_value,new_value)
func _on_data_changed(tag_space:String,tag:String,old_value,new_value):
	super(tag_space,tag,old_value,new_value)
#endregion
#region 公共方法
## 得到最终速度
func get_final_move_vel_info(delta:float = 0.02) -> Dictionary:
	var cur_base_vel = get_attribute_value("BaseVel")
	var cur_acc = get_attribute_value("Acc")
	var move_speed_scale = get_attribute_value("MoveSpeedScale")
	var acc = Vector2(cur_acc.x,cur_acc.y)
	var vel:Vector2 = (cur_base_vel + acc * delta)
	if vel.length() > 0:
		var move_dir = vel.normalized()
		var friction_acc = get_attribute_value("FrictionAcc") * -move_dir
		vel += friction_acc * delta * min(vel.length()/(friction_acc * delta).length(),1)
	vel = vel.normalized() * clamp(vel.length(),0,get_attribute_value("MaxMoveSpeed"))
	var vel_info = {
		BaseVel = vel,
		FinalVel = vel * move_speed_scale,
	}
	return vel_info
## 获得相关的LayerMask
func get_target_layer_mask() -> int:
	return 0
func get_obstacle_layer_mask() -> int:
	return 0
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _init() -> void:
	super()
	if Engine.is_editor_hint():
		if not attribute_set:
			attribute_set = AttributeSet.new()
		attribute_set.resource_local_to_scene = true
		add_attribute_value("MaxHealth",CompositeProperty.PropertyType.Int,100)
		add_attribute_value("MaxMoveSpeed",CompositeProperty.PropertyType.Float,100)
		add_attribute_value("MoveSpeedAcc",CompositeProperty.PropertyType.Float,400)
		add_attribute_value("MoveSpeedScale",CompositeProperty.PropertyType.Float,1)
		add_attribute_value("FrictionAcc",CompositeProperty.PropertyType.Float,200)
		add_attribute_value("WeaponMaxAmount",CompositeProperty.PropertyType.Float,8)
		add_attribute_value("MaxStamina",CompositeProperty.PropertyType.Float,100)
		add_attribute_value("StaminaRecoverPerSecond",CompositeProperty.PropertyType.Float,20)
#endregion
